package tx.bj12320.action;

import org.apache.struts2.ServletActionContext;

import cn.org.rapid_framework.web.scope.Flash;

import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.ModelDriven;

import java.io.File;
import java.util.*;

import javacommon.base.*;

import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;

import tx.bj12320.model.*;
import tx.bj12320.model.enums.BooleanEnum;
import tx.bj12320.model.enums.SysUserTypeEnum;

import tx.bj12320.service.*;
import tx.bj12320.util.SpringSecurityUtils;
import tx.bj12320.util.WebUtils;
import tx.bj12320.vo.query.*;

public class InfoissueAction extends BaseStruts2Action implements Preparable,
		ModelDriven {
	// 默认多列排序,example: username desc,createTime asc
	public static final String DEFAULT_SORT_COLUMNS = "infoissueId DESC";

	// forward paths
	protected static final String QUERY_JSP = "/pages/Infoissue/query.jsp";
	protected static final String LIST_JSP = "/pages/Infoissue/list.jsp";
	protected static final String CREATE_JSP = "/pages/Infoissue/create.jsp";
	protected static final String EDIT_JSP = "/pages/Infoissue/edit.jsp";
	protected static final String SHOW_JSP = "/pages/Infoissue/show.jsp";
	protected static final String VIEW_JSP = "/infoissue-for-view.jsp";
	// redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/pages/Infoissue/list.do";
	// 新增普通用户浏览页面
	protected static final String USER_LIST_JSP = "/infoissue-for-browse.jsp";
	private InfoissueManager infoissueManager;
	private UserInfoManager userInfoManager; // 2011-7-8 getter/setter

	private InfoissueViewLogManager infoissueViewLogManager;
	public void setInfoissueViewLogManager(InfoissueViewLogManager manager) {
		this.infoissueViewLogManager = manager;
	}

	public UserInfoManager getUserInfoManager() {
		return userInfoManager;
	}

	public void setUserInfoManager(UserInfoManager userInfoManager) {
		this.userInfoManager = userInfoManager;
	}

	private Infoissue infoissue;
	java.lang.Long id = null;
	private String SpecialtyInfoissueId;
	public String getSpecialtyInfoissueId() {
		return SpecialtyInfoissueId;
	}

	public void setSpecialtyInfoissueId(String specialtyInfoissueId) {
		SpecialtyInfoissueId = specialtyInfoissueId;
	}

	private String[] items;
	private String[] reportUnit_select;
	private String mail_infoTitle;
	private String mail_issueDateString;
	private String mail_infoContent;
	private String mail_attachmentName;
	private String replymail;// 判断是否回复信息
	private String attachmentName;
	public String getAttachmentName() {
		return attachmentName;
	}

	public void setAttachmentName(String attachmentName) {
		this.attachmentName = attachmentName;
	}

	public String[] getAttachmentNames() {
		return attachmentNames;
	}

	public void setAttachmentNames(String[] attachmentNames) {
		this.attachmentNames = attachmentNames;
	}

	public File[] getAttachmentFiles() {
		return attachmentFiles;
	}

	public void setAttachmentFiles(File[] attachmentFiles) {
		this.attachmentFiles = attachmentFiles;
	}

	public String[] getAttachmentFilesContentType() {
		return attachmentFilesContentType;
	}

	public void setAttachmentFilesContentType(String[] attachmentFilesContentType) {
		this.attachmentFilesContentType = attachmentFilesContentType;
	}

	public String[] getAttachmentFilesFileName() {
		return attachmentFilesFileName;
	}

	public void setAttachmentFilesFileName(String[] attachmentFilesFileName) {
		this.attachmentFilesFileName = attachmentFilesFileName;
	}

	private String[] attachmentNames;
	private File[] attachmentFiles;
	private String[] attachmentFilesContentType;
	private String[] attachmentFilesFileName;
	public String getReplymail() {
		return replymail;
	}

	public void setReplymail(String replymail) {
		this.replymail = replymail;
	}

	public String getMail_attachmentName() {
		return mail_attachmentName;
	}

	public void setMail_attachmentName(String mailAttachmentName) {
		mail_attachmentName = mailAttachmentName;
	}

	public String getMail_infoTitle() {
		return mail_infoTitle;
	}

	public void setMail_infoTitle(String mailInfoTitle) {
		mail_infoTitle = mailInfoTitle;
	}

	public String getMail_issueDateString() {
		return mail_issueDateString;
	}

	public void setMail_issueDateString(String mailIssueDateString) {
		mail_issueDateString = mailIssueDateString;
	}

	public String getMail_infoContent() {
		return mail_infoContent;
	}

	public void setMail_infoContent(String mailInfoContent) {
		mail_infoContent = mailInfoContent;
	}

	public String[] getReportUnit_select() {
		return reportUnit_select;
	}

	public void setReportUnit_select(String[] reportUnitSelect) {
		reportUnit_select = reportUnitSelect;
	}

	private File infoissueFile;
	private String infoissueFileContentType;
	private String infoissueFileFileName;

	private Integer userInSession_id;// 传递隐藏bjmdr_id
	private String userInSession_name;// 传递隐藏bjmdr_name

	public String getUserInSession_name() {
		return userInSession_name;
	}

	public void setUserInSession_name(String userInSessionName) {
		userInSession_name = userInSessionName;
	}

	public Integer getUserInSession_id() {
		return userInSession_id;
	}

	public void setUserInSession_id(Integer userInSessionId) {
		userInSession_id = userInSessionId;
	}

	public File getInfoissueFile() {
		return infoissueFile;
	}

	public void setInfoissueFile(File infoissueFile) {
		this.infoissueFile = infoissueFile;
	}

	public String getInfoissueFileContentType() {
		return infoissueFileContentType;
	}

	public void setInfoissueFileContentType(String infoissueFileContentType) {
		this.infoissueFileContentType = infoissueFileContentType;
	}

	public String getInfoissueFileFileName() {
		return infoissueFileFileName;
	}

	public void setInfoissueFileFileName(String infoissueFileFileName) {
		this.infoissueFileFileName = infoissueFileFileName;
	}

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			infoissue = new Infoissue();
		} else {
			infoissue = (Infoissue) infoissueManager.getById(id);
		}
	}

	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setInfoissueManager(InfoissueManager manager) {
		this.infoissueManager = manager;
	}

	public Object getModel() {
		return infoissue;
	}

	public void setInfoissueId(java.lang.Long val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	/** 执行搜索 */
	public String list() {
		InfoissueQuery query = newQuery(InfoissueQuery.class,
				DEFAULT_SORT_COLUMNS);
		query.setIsMail(new Long(0));
		query.setIsInfoToPublish(BooleanEnum.FALSE.getCode());
		Page page = infoissueManager.findPage(query);
		savePage(page, query);
		return LIST_JSP;
	}

	public String to_publish_list() {
		InfoissueQuery query = newQuery(InfoissueQuery.class,
				DEFAULT_SORT_COLUMNS);
		query.setIsMail(new Long(0));
		query.setIsInfoToPublish(BooleanEnum.TRUE.getCode());
		if(!WebUtils.getUserInSession().getSysUserType().equals(SysUserTypeEnum.BJ12320.getCode())){
			query.setUserInfoId(WebUtils.getUserInSession().getUserInfoId());
		}
		Page page = infoissueManager.findPage(query);
		savePage(page, query);
		return "/pages/Infoissue-to-publish/list.jsp";
	}

	/** 站内信功能 */
	public String mail_list() {
		InfoissueQuery query = newQuery(InfoissueQuery.class,
				DEFAULT_SORT_COLUMNS);
		query.setIsMail(new Long(1));
		Page page = infoissueManager.findPage(query);
		savePage(page, query);
		return "/infoissue-mail-for-browse.jsp";
	}

	/**
	 * 采纳信息上报
	 * @return
	 */
	public String adoptPublishInfo(){
		infoissue.setIsAdopt(new Long(1));
		infoissueManager.update(this.infoissue);
		Flash.current().success("采纳信息成功！");
		return "/pages/Infoissue/to_publish_list.do";
	}

	/*
	 * 收件箱
	 */
	public String receiveMail_list() {
		InfoissueQuery query = newQuery(InfoissueQuery.class,
				DEFAULT_SORT_COLUMNS);
		query.setIsMail(new Long(1));
		query.setMailReceiverId(WebUtils.getUserInSession().getOrgInfoId());
		if (!SpringSecurityUtils.isAnonymousUser()) {
			if (!WebUtils.is12320User()) {
				query.setMailReceiverType(SysUserTypeEnum.HOSPITAL.getCode()+"__"+SysUserTypeEnum.SECTION_BOARD_OF_HEALTH.getCode());
			} else if (WebUtils.is12320User()) {
				query.setMailReceiverType(SysUserTypeEnum.BJ12320.getCode());
			}
		}

		Page page = infoissueManager.findPage(query);
		savePage(page, query);
		return "/infoissue-receiveMail-for-browse.jsp";
	}

	/*
	 * 发件箱
	 */
	public String sendMail_list() {
		InfoissueQuery query = newQuery(InfoissueQuery.class,
				DEFAULT_SORT_COLUMNS);
		query.setIsMail(new Long(1));
		query.setMailReceiverId(WebUtils.getUserInSession().getOrgInfoId());
		if (!SpringSecurityUtils.isAnonymousUser()) {
			if (!WebUtils.is12320User()) {
				query.setMailReceiverType(SysUserTypeEnum.HOSPITAL.getCode()+"__"+SysUserTypeEnum.SECTION_BOARD_OF_HEALTH.getCode());
			} else if (WebUtils.is12320User()) {
				query.setMailReceiverType(SysUserTypeEnum.BJ12320.getCode());
			}
		}
		query.setIsMainMail(new Long(1));
		Page page = infoissueManager.findPage(query);
		savePage(page, query);
		return "/infoissue-sendMail-for-browse.jsp";
	}

	/** 普通用户执行搜索 */
	public String role_report_unit_list() {
		InfoissueQuery query = newQuery(InfoissueQuery.class,
				DEFAULT_SORT_COLUMNS);
		query.setIsMail(new Long(0));
		Page page = infoissueManager.findPage(query);
		savePage(page, query);
		return USER_LIST_JSP;
	}

	/** 查看对象 */
	public String show() {
		InfoissueViewLogQuery query = newQuery(InfoissueViewLogQuery.class,DEFAULT_SORT_COLUMNS);
		query.setUserInfoId(WebUtils.getUserInSession().getUserInfoId());
		query.setInfoissueId(id);
		Page page = infoissueViewLogManager.findPage(query);
		if(page.getResult().size()==0){
			InfoissueViewLog ivl=new InfoissueViewLog();
			ivl.setInfoissueId(id);
			ivl.setUserInfoId(WebUtils.getUserInSession().getUserInfoId());
			ivl.setViewLogDate(new Date());
			infoissueViewLogManager.save(ivl);
		}

		return SHOW_JSP;
	}

	public String to_publish_show() {
		return "/pages/Infoissue-to-publish/show.jsp";
	}

	public String view() {
		/* 下列判断是已读，未读状态。 */
		if (infoissue.getIsRead() != null && infoissue.getIsRead() == 0) {// 点击查看变成已读状态
			infoissue.setIsRead(new Long(1));
			infoissue.setReadDate(new Date());
			infoissueManager.saveOrUpdate(infoissue);
		}
		return VIEW_JSP;
	}

	/** 发送人查看邮箱信息 */
	public String mail_view() {
		if (!SpringSecurityUtils.isAnonymousUser()) {
			 if (WebUtils.is12320User()) {
				List receivers = infoissueManager.findReceivePerson(infoissue
						.getInfoissueId());
				getRequest().setAttribute("page", receivers);
			}

		}
		return VIEW_JSP;
	}

	// public String receivemail_view() {
	// return VIEW_JSP;
	// }
	/** 进入新增页面 */
	public String create() {
		if (!SpringSecurityUtils.isAnonymousUser()) {
				userInSession_name = WebUtils.getUserInSession().getUserName();
		}
		return CREATE_JSP;
	}
	public String to_publish_create() {
		if (!SpringSecurityUtils.isAnonymousUser()) {
				userInSession_name = WebUtils.getUserInSession().getUserName();
		}
		return "/pages/Infoissue-to-publish/create.jsp";
	}

	/** 进入站内信新增页面 */
	public String mail_add() {
		if (replymail != null) {
			replymail = "回复:" + infoissue.getInfoTitle();
			if (infoissue.getIsRead() != null && infoissue.getIsRead() == 0) {// 点击查看变成已读状态
				infoissue.setIsRead(new Long(1));
				infoissue.setReadDate(new Date());
				infoissueManager.saveOrUpdate(infoissue);
			}
		}

		if (!SpringSecurityUtils.isAnonymousUser()) {
			userInSession_name = WebUtils.getUserInSession().getUserName();
		}
		return "/infoissue-mail-for-add.jsp";
	}

	/** 保存新增对象 */
	public String save() {
		/* 保存新增对象的用户ID start */
		infoissue.setUserInfoId(WebUtils.getUserInSession().getUserInfoId());
		infoissue.setIsMail(new Long(0));
		infoissue.setIsInfoToPublish(BooleanEnum.FALSE.getCode());
		infoissueManager.save(infoissue);
		/* end */
		if (infoissueFile != null) {
			String fileFormat = WebUtils.getExtention(infoissueFileFileName);
			String newFileName = infoissue.getInfoissueId() + "_" + "info"
					+ fileFormat;
			File newFile = new File(ServletActionContext.getServletContext()
					.getRealPath("/infoissue-files")
					+ "/" + newFileName);
			WebUtils.copy(infoissueFile, newFile);

			infoissue.setAttachmentFile(newFileName);
		}
		infoissueManager.saveOrUpdate(infoissue);
		Flash.current().success(CREATED_SUCCESS); // 存放在Flash中的数据,在下一次http请求中仍然可以读取数据,error()用于显示错误消息
		return LIST_ACTION;
	}

	public String to_publish_save() {
		/* 保存新增对象的用户ID start */
		infoissue.setUserInfoId(WebUtils.getUserInSession().getUserInfoId());
		infoissue.setIsMail(new Long(0));
		infoissue.setIsInfoToPublish(BooleanEnum.TRUE.getCode());
		infoissueManager.save(infoissue);
		/* end */
		if (infoissueFile != null) {
			String fileFormat = WebUtils.getExtention(infoissueFileFileName);
			String newFileName = infoissue.getInfoissueId() + "_" + "info"
					+ fileFormat;
			File newFile = new File(ServletActionContext.getServletContext()
					.getRealPath("/infoissue-files")
					+ "/" + newFileName);
			WebUtils.copy(infoissueFile, newFile);

			infoissue.setAttachmentFile(newFileName);
		}
		infoissueManager.saveOrUpdate(infoissue);
		Flash.current().success(CREATED_SUCCESS); // 存放在Flash中的数据,在下一次http请求中仍然可以读取数据,error()用于显示错误消息
		return "!/pages/Infoissue/to_publish_list.do";
	}

	public String mail_reportUnitInfo_select() {

		return "/reportUnitInfo-select.jsp";
	}

	/* 站内信保存 */
	public String mail_save() {
		/* 保存新增对象的用户ID start */
		// infoissue.setInfoissueId(null); //防止数据更新而未新增

		if (!SpringSecurityUtils.isAnonymousUser()) {
			if (!WebUtils.is12320User()) {
				boolean isMainBoolean = true;
				boolean isMainInfoissueId=true;
				Long mainInfoissueid = new Long(100);
				List<UserInfo> RoleUserlist = new ArrayList<UserInfo>();
				RoleUserlist = userInfoManager.findAll();// 查询所有权限拥护
				for (UserInfo user : RoleUserlist) {
					Infoissue infoissue = new Infoissue();
					infoissue.setMailReceiverType(SysUserTypeEnum.BJ12320.getCode());
					infoissue.setInfoTitle(mail_infoTitle);
					infoissue.setInfoContent(mail_infoContent);
					infoissue.setIssueDateString(mail_issueDateString);
					 infoissue.setIsMail(new Long(1));
					infoissue.setIsRead(new Long(0));// 默认未读
					if(isMainBoolean) infoissue.setIsMainMail(new Long(1)); //是否为主邮件

					infoissue.setMailReceiverId(user.getUserInfoId());//
					infoissue.setMailReceiverSum(new Long(RoleUserlist.size()));//
					infoissueManager.save(infoissue);
					if(isMainInfoissueId)
						 mainInfoissueid =infoissue.getInfoissueId();
					if(attachmentFiles!=null){
						StringBuffer attachmentNameArray = new StringBuffer();
						StringBuffer attachmentFilesArray = new StringBuffer();
						for(int i = 0; i < attachmentFiles.length; i++) {
							String fileFormat=WebUtils.getExtention(attachmentFilesFileName[i]);

							String newFileName = mainInfoissueid + "_"+i+fileFormat;
							if(infoissue.getInfoissueId()!=mainInfoissueid) {
							File newFile = new File(ServletActionContext.getServletContext()
									.getRealPath("/infoissue-files")
									+ "/" + newFileName);
							WebUtils.copy(attachmentFiles[i], newFile);
							}
							attachmentNameArray.append(attachmentFilesFileName[i]+"||");
							attachmentFilesArray.append(newFileName+"||");
						}
						infoissue.setAttachmentName(attachmentNameArray.toString());
						infoissue.setAttachmentFile(attachmentFilesArray.toString());
					}

					infoissueManager.saveOrUpdate(infoissue);
					isMainBoolean=false;
					isMainInfoissueId=false;
				}
			}
		}



		if (!SpringSecurityUtils.isAnonymousUser()) {
			if (WebUtils.is12320User()) {
				boolean isMainBoolean = true;
				boolean isMainInfoissueId=true;
				Long mainInfoissueid = new Long(100);
				if (reportUnit_select != null && reportUnit_select.length > 0) {
					for (int i = 0; i < reportUnit_select.length; i++) {
						Infoissue infoissue_other = new Infoissue();
						infoissue_other
								.setMailReceiverType(SysUserTypeEnum.BJ12320.getCode());
						infoissue_other.setInfoTitle(mail_infoTitle);
						infoissue_other.setInfoContent(mail_infoContent);
						infoissue_other
								.setIssueDateString(mail_issueDateString);
						infoissue_other.setIsMail(new Long(1));
						infoissue_other.setIsRead(new Long(0));// 默认未读
						infoissue_other.setMailReceiverId(new Long(
								reportUnit_select[i]));
						if( isMainBoolean ) infoissue_other.setIsMainMail(new Long(1));
						infoissue_other
								.setMailReceiverSum(new Long(reportUnit_select.length));
						infoissueManager.save(infoissue_other);
						if(isMainInfoissueId)
							 mainInfoissueid =infoissue_other.getInfoissueId();

						if(attachmentFiles!=null){
							StringBuffer attachmentNameArray = new StringBuffer();
							StringBuffer attachmentFilesArray = new StringBuffer();
							for(int j = 0; j < attachmentFiles.length; j++) {
								String fileFormat=WebUtils.getExtention(attachmentFilesFileName[j]);

								String newFileName = mainInfoissueid + "_"+j+fileFormat;
								if(infoissue_other.getInfoissueId()!=mainInfoissueid) {
								File newFile = new File(ServletActionContext.getServletContext()
										.getRealPath("/infoissue-files")
										+ "/" + newFileName);
								WebUtils.copy(attachmentFiles[j], newFile);
								}
								attachmentNameArray.append(attachmentFilesFileName[j]+"||");
								attachmentFilesArray.append(newFileName+"||");
							}
							infoissue_other.setAttachmentName(attachmentNameArray.toString());
							infoissue_other.setAttachmentFile(attachmentFilesArray.toString());
						}

						infoissue_other.setMainMailInfoissueId(mainInfoissueid);
						infoissueManager.saveOrUpdate(infoissue_other);
						isMainBoolean = false;
						isMainInfoissueId=false;
					}
				}
			}

		}
		/* end */

		Flash.current().success(CREATED_SUCCESS); // 存放在Flash中的数据,在下一次http请求中仍然可以读取数据,error()用于显示错误消息
		return "!/pages/Infoissue/sendMail_list.do";
	}

	/** 进入更新页面 */
	public String edit() {
		userInSession_name = WebUtils.getUserInSession().getUserName(); // 传递默认用户名
		return EDIT_JSP;
	}

	public String to_publish_edit() {
		userInSession_name = WebUtils.getUserInSession().getUserName(); // 传递默认用户名
		return "/pages/Infoissue-to-publish/edit.jsp";
	}

	/** 保存更新对象 */
	public String update() {
		if (infoissueFile != null) {
			String fileFormat = WebUtils.getExtention(infoissueFileFileName);
			String newFileName = infoissue.getInfoissueId() + "_" + "info"
					+ fileFormat;
			File newFile = new File(ServletActionContext.getServletContext()
					.getRealPath("/sup-infoissue-files")
					+ "/" + newFileName);
			WebUtils.copy(infoissueFile, newFile);

			infoissue.setAttachmentFile(newFileName);
		}
		infoissueManager.update(this.infoissue);
		Flash.current().success(UPDATE_SUCCESS);
		return LIST_ACTION;
	}

	/** 删除对象 */
	public String delete() {
		for (int i = 0; i < items.length; i++) {
			Hashtable params = HttpUtils.parseQueryString(items[i]);
			java.lang.Long id = new java.lang.Long((String) params
					.get("infoissueId"));
			infoissueManager.removeById(id);
		}
		Flash.current().success(DELETE_SUCCESS);
		return "!/pages/Infoissue/querySpecialtyMonitorWorkBrowse.do";
	}


	/** 专项检测*/
	public String querySpecialtyMonitorWorkBrowse() {
		InfoissueQuery query = newQuery(InfoissueQuery.class,
				DEFAULT_SORT_COLUMNS);
		query.setIsMail(new Long(2));//
		Page page = infoissueManager.findPage(query);
		savePage(page, query);
		return "/query-specialty-monitor-work-for-browse.jsp";
	}
	public String querySpecialtyMonitorWorkToAdd() {

		return "/specialty-monitor-work-for-add.jsp";
	}
	public String querySpecialtyMonitorWorkAdd() {
		infoissue.setIsMail(new Long(2));
		infoissueManager.save(infoissue);
		return "!/pages/Infoissue/querySpecialtyMonitorWorkBrowse.do";
	}
	public String querySpecialtyMonitorWork_reportUnitToAdd() {
		getRequest().setAttribute("SpecialtyInfoissueId", SpecialtyInfoissueId);
		return "/specialty-monitor-work-for-reportUnit-add.jsp";
	}
	public String querySpecialtyMonitorWork_reportUnitAdd() {
		Infoissue infoissueOld= infoissueManager.getById( new Long(SpecialtyInfoissueId));

		Infoissue infoissue_ru= new Infoissue();
		infoissue_ru.setIsMail(new Long(3)); //回复3
		infoissue_ru.setIsMainMail(infoissueOld.getInfoissueId());
		infoissue_ru.setAttachmentName(attachmentName);
		infoissue_ru.setIssueDate( new Date());
		infoissueManager.save(infoissue_ru);
		if (infoissueFile != null) {
			String fileFormat = WebUtils.getExtention(infoissueFileFileName);
			String newFileName = infoissue_ru.getInfoissueId() + "_" + "info"
					+ fileFormat;
			File newFile = new File(ServletActionContext.getServletContext()
					.getRealPath("/infoissue-files")
					+ "/" + newFileName);
			WebUtils.copy(infoissueFile, newFile);

			infoissue_ru.setAttachmentFile(newFileName);
		}
		infoissueManager.saveOrUpdate(infoissue_ru);
		Flash.current().success(CREATED_SUCCESS);

		return "!/pages/Infoissue/querySpecialtyMonitorWorkBrowse.do";
	}

	public String  querySpecialtyMonitorWorkToView() {

		InfoissueQuery query = newQuery(InfoissueQuery.class,DEFAULT_SORT_COLUMNS,10000);
		query.setIsMail(new Long(3));
		query.setIsMainMail(infoissue.getInfoissueId());
		Page page = infoissueManager.findPage(query);
		savePage(page, query);
		return "/specialty-monitor-work-for-reportUnit-view.jsp";
	}
}
